#include<stdio.h>
#include <stdlib.h>

int* array;
int n,i,j,a;
int temp,k,number,val;


int extra=0;
int position,numins;

int pos1,l,w;

int m ;



void append()

{
    

  

  {             
                {
                
                array=realloc(array , (j+1)*sizeof(int));
                printf("enter a number : ");
                scanf("%d",&n);
                
                array[j]=n;
                
                j++;
                
                
                
                }
                printf("\n");
                display();
                
                
   }
   

}



void count()
{   int elem=0;
    for(i=0;i<j;i++)
    {
                    elem++;
    }
    printf("total number of elements in the list are %d : ",elem);
    
    printf("\n");
}

void value()
{
    
    printf("enter the number whose index you want to find : ");
    scanf("%d",&number);
    for(i=0;i<j;i++)
    {
                    if (array[i]==number)
                    {
                    printf("the index of %d is %d",number,i);
                    break;
                    }
    }
}






void display()
{
    for(i=0;i<j;i++)
    {
               printf(" %d ,",array[i]);
    }
    printf("\n");
}

void extend()
{

            printf("Enter the number of elements you want to extend in the list");
            scanf("%d",&extra);
            array=realloc(array , (j+extra)*sizeof(int));
                      
            for(k=j;k<j+extra;k++)
            {
                                  printf("Enter the elements");
                                  scanf("%d",&array[k]);
            }
            printf("%d",extra);
            printf("array is: \n");
            for (i=0;i<j+extra;i++)
                {
                printf(" %d ,",array[i]);
                }
                j=j+extra;
}


void sort()
    
    {     int k,temp;
          for(i=0;i<j;i++)
          {
                           for(k=i+1;k<j;k++)
                           {
                                              if(array[i]>array[k])
                                              {
                                                                   temp=array[i];
                                                                   array[i]=array[k];
                                                                   array[k]=temp;
                                              }
                           }
          }
          display();
    }
    
    void insert()
{
    printf("enter the position at which u want to insert");
    scanf("%d",&position);
    printf("enter the number you want to insert\n");
    scanf("%d",&numins);
    if(position<0 ||position>(j-1))
    {
    printf("cant insert at specified position\n");
    
    
    }
    for (i=j;i>position;i--)
    {
     array[i]=array[i-1];
     }
     array[position]=numins;
     
    
     j=(j+1);
     
     display();
}


void pop()
{   
    printf("do you want to enter index 1 for yes,0 for no ");
    scanf("%d",&w);
    switch(w)
    {
    case(1):
    {
         printf("enter the position/index from where you want to remove the element");
    scanf("%d",&pos1);
    
    printf("the element at the given position was : %d \n",array[pos1]);
                      for(l=pos1;l<j;l++)
                      {
                                             array[l]=array[l+1];
                      }
                      j=j-1;
                      display();
                      break;
    }
    case(0):
    {
         printf("element at last position was %d \n",array[j-1]);
        
         j=j-1;
         display();
         break;
    }
    }

}

void delete()
{
printf("enter the number you wish to delete \n");
scanf("%d",&w);
for(i=0;i<j;i++){
    if(w==array[i]){
        pos1=i;
        for(l=pos1;l<j;l++)
                      {
                          array[l]=array[l+1];
                      }
                      j=j-1;
                      display();
                      break;
    }
}
}
 
 
void reverse()
{
     
    for(i=0,m=j;i<(j/2);i++,m--)
    {
    int temp;
    temp=array[i];
    array[i]=array[m-1];
    array[m-1]=temp;
     
    }
    display();
 
 
}





//MAIN PROGRAM

  int main()
 {
        int k;
        array = (int*) malloc((j+1)*sizeof(int));
   
   while(1) /* this is an indefinite loop */
 {  
            printf("\n");
            
    printf("1.append function..;\n");
    printf("2.count function :\n");
    printf("3.index function :\n");
    
    printf("4.sort function :\n");
    printf("5.extend function :\n");
    
    printf("6.insert function :\n");
    
    printf("7.pop function :\n");
    printf("8.delete function \n");
     printf("9.reverse function \n");   
   
    printf("10.to display the list\n");
    printf("11.press 11 to exit the program:\n");
    printf("\n");
    printf("enter a number according to the function you want to perform( from above):\n");

    scanf("%d",&k); 
    printf("\n");

      switch(k)
    {
       //adds the number entered to the end of the linkedlist
          case 1:
       {
         
         append();
         break;
       }

//counts the number of elements in the list
      case 2:
   {
      count();
      break;
   } 
      

    
     
//returns the index of specified number
   case 3:
        {
              value();
              break;
        }
        
        //to sort a list        
   case 4:
        {
              sort();
              break;
        }
   
 
 //to extend a list
 case 5:
        {
              extend();
              break;
        }
        
 
 //to insert a no        
   case 6:
       {
         
         insert();
         break;
       }
       
 //pop function
 
       case 7:
            {
                  pop();
                  break;
            }
            
       
     //remove function
    case 8: delete();
        break;     
       
            
            
            
//reverse function
    case 9: reverse();
        break;     
       
            
            
            

        
 //to display the elements of the list
    case 10:
      {
      
         
         display();
         break;
      }
  //to exit the program
  case 11:
 {
  exit(0);
 }
    }
 }
 }
